<?php
namespace bank;
use Exception;
use people\Client;
use products\ConsumerCredit;
use products\HomeCredit;
use products\ShortDeposit;
use products\LongDeposit;

class Bank {
	
	private $bankName;
	private $bankAddress;
	private $availableMoney;
	private $bankDeposits = array();
	private $bankCredits = array();
	private $bankReserve = 0;
	private $depositNumbers = 1;
	private $creditNumbers = 1;
	
	public function __construct($bankName, $bankAddress, $availableMoney) {
		
		$this->setBankName($bankName);
		$this->setBankAddress($bankAddress);
		$this->setAvailableMoney($availableMoney);
		echo "<p>Opening ".$this->bankName."</p>";
	}
	
	public function setBankName($bankName) {
		
		if($bankName == "" || $bankName == NULL) {
				
			throw new Exception("Bank name parameter can't be null or empty string");
		}
		
		if(!is_string($bankName)) {
				
			throw new Exception("Bank name parameter mus be a string value!");
		}
		
		$this->bankName = $bankName;
	}
	
	public function getBankName() {
		
		return $this->bankName;
	}
	
	public function setBankAddress($bankAddress) {
	
		if($bankAddress == "" || $bankAddress == NULL) {
	
			throw new Exception("Bank address parameter can't be null or empty string");
		}
	
		if(!is_string($bankAddress)) {
	
			throw new Exception("Bank address parameter mus be a string value!");
		}
	
		$this->bankAddress = $bankAddress;
	}
	
	public function getBankAddress() {
	
		return $this->bankAddress;
	}
	
	public function setAvailableMoney($availableMoney) {
		
		$this->availableMoney = $availableMoney;
	}
	
	public function getAvailableMoney() {
		
		return $this->availableMoney;
	}
	
	public function getBankReserve() {
		
		return $this->bankReserve;
	}
	
	public function addDeposit($depositType, $depositAmount, Client $client) {
		
		if (!is_numeric($depositAmount) || $depositAmount<0) {
				
			throw new Exception("Inavlid value for deposit amount!");
				
		}
		if($depositAmount > $client->getCash()) {
				
			throw new Exception($client->getName() . ", you don't have enough money for this deposit!");
		}
		
		if($depositType != "ShortDeposit" && $depositType != "LongDeposit") {
				
			throw new Exception("Invalid deposit type!");
		}
		
		$depositName = "D" . $this->depositNumbers;
		$this->depositNumbers++;
		
		if($depositType == "ShortDeposit") {
		
			$this->bankDeposits[$depositName] = new ShortDeposit($depositName, $depositAmount);
		
		} else if ($depositType == "LongDeposit") {
		
			$this->bankDeposits[$depositName] = new LongDeposit($depositName, $depositAmount);
		}
		
		$client->addDeposit($this->bankDeposits[$depositName]);
		$this->availableMoney += $depositAmount;
		$this->bankReserve += (10/100) * $depositAmount;
	}
	
	public function addCredit($creditType, $creditAmount, $creditPeriod, Client $client) {
		
		$creditName = "C" . $this->creditNumbers;
		$this->creditNumbers++;
		
		if (!is_numeric($creditAmount) || $creditAmount < 0) {
		
			throw new Exception("Inavlid value for credit amount!");
		
		}
		
		$currentPayments = 0;
		
		foreach($client->getCredits() as $credit) {
				
			$currentPayments+=$credit->getMonthlyPayment;
		}
		
		if($currentPayments > ((1/2)*$client->getSalary())) {
				
			throw new Exception("You can't afford more credits!");
				
		}
		
		if(($this->availableMoney - $creditAmount) < $this->bankReserve) {
			
			throw new Exception("Bank don't have enough money to lend you this credit, ". $client->getName() ."!");
		}
		
		if($creditType == "ConsumerCredit") {
			
			$this->bankCredits[$creditName] = new ConsumerCredit($creditName, $creditPeriod, $creditAmount);
			
		}
		
		if($creditType == "HomeCredit") {
				
			$this->bankCredits[$creditName] = new HomeCredit($creditName, $creditPeriod, $creditAmount);	
		}
		
		$this->availableMoney-=$creditAmount;
		$client->setCash($client->getCash()+$creditAmount);
		$client->addCredit($this->bankCredits[$creditName]);
		
	}
	
	public function __toString() {
		
		$str = "";
		$str .= "<p>Bank: ".$this->bankName."</p>";
		$str .= "<p>Available money: ".$this->availableMoney."</p>";
		$str .= "<p>Reserve: ".$this->bankReserve."</p>";
		if (sizeof($this->bankDeposits) > 0) {
			
			$str .= "<p>Deposits: </p>";
			foreach($this->bankDeposits as $deposit) {
				
				$str .= "<p> Deposit ". $deposit->getAccountName()." - Available money: ". $deposit->getCurrentMoneyAmount()."</p>";
			}
		} else {
			
			$str .= "<p>There are no available deposits!</p>";
		}
		
		if (sizeof($this->bankCredits) > 0) {
				
			$str .=  "<p>Credits: </p>";
			foreach($this->bankCredits as $credit) {
		
				$str .= "<p> Credit ". $credit->getAccountName()." - Money granted: ". $credit->getCurrentMoneyAmount()." - Current Debt: ".$credit->getCurrentDebt()."</p>";
			}
		} else {
			
			$str .= "<p>There are no available credits!</p>";
		}
		
		return $str;
	}
}